package com.itheima.a13;

import org.springframework.cglib.core.Signature;

public class TargetFastClass {
    /*    static Signature s0 = new Signature("save", "()V");
        static Signature s1 = new Signature("save", "(I)V");
        static Signature s2 = new Signature("save", "(J)V");

        // 获取目标方法的编号

            Target
                save()              0
                save(int)           1
                save(long)          2
            signature 包括方法名字、参数返回值

        public int getIndex(Signature signature) {
            if (s0.equals(signature)) {
                return 0;
            } else if (s1.equals(signature)) {
                return 1;
            } else if (s2.equals(signature)) {
                return 2;
            }
            return -1;
        }

        // 根据方法编号, 正常调用目标对象方法
        public Object invoke(int index, Object target, Object[] args) {
            if (index == 0) {
                ((Target) target).save();
                return null;
            } else if (index == 1) {
                ((Target) target).save((int) args[0]);
                return null;
            } else if (index == 2) {
                ((Target) target).save((long) args[0]);
                return null;
            } else {
                throw new RuntimeException("无此方法");
            }
        }

        public static void main(String[] args) {
            TargetFastClass fastClass = new TargetFastClass();
            int index = fastClass.getIndex(new Signature("save", "(I)V"));
            System.out.println(index);
            fastClass.invoke(index, new Target(), new Object[]{100});
        }*/
    static Signature s0 = new Signature("save","()V");
    static Signature s1 = new Signature("save","(I)V");
    static Signature s2 = new Signature("save","(J)V");
    // 获取目标方法的编号
    /*
    *   Target
    *       save()          0
    *       save(int)       1
    *       save(long)      2
    *   signature 包括方法的名字,参数,返回值
    * */
    public int getIndex(Signature signature) {
        if (s0.equals(signature)){
            return 0;
        } else if (s1.equals(signature)) {
            return 1;
        } else if (s2.equals(signature)) {
            return 2;
        }
        return -1;
    }
    // 根据getIndex返回的方法编号,正常调用目标对象的方法
    public Object invoke(int index, Object target, Object[] args){
        if (index == 0){
            ((Target) target).save();
            return null;
        } else if (index == 1) {
            ((Target) target).save(((int) args[0]));
            return null;
        } else if (index == 2) {
            ((Target) target).save(((long) args[0]));
            return null;
        }else{
            throw new RuntimeException("无此方法");
        }
    }

    public static void main(String[] args) {
        TargetFastClass fastClass = new TargetFastClass();
        int index1 = fastClass.getIndex(new Signature("save", "()V"));
        int index2 = fastClass.getIndex(new Signature("save", "(I)V"));
        int index3 = fastClass.getIndex(new Signature("save", "(J)V"));
        System.out.println(index1);
        System.out.println(index2);
        System.out.println(index3);
        fastClass.invoke(index1,new Target(),new Object[0]);
        fastClass.invoke(index2,new Target(),new Object[]{100});
        fastClass.invoke(index3,new Target(),new Object[]{100L});
    }
}
